
package com.ifcar.app.activity;

import com.ifcar.app.activity.main.HomeMainFragment;
import com.ifcar.app.service.ClearCacheService;
import com.ifcar.app.util.ThemeSettingsHelper;
import com.ifcar.app.activity.R;
import android.app.AlertDialog;
import android.app.NotificationManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewStub;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

public class HomePage extends BaseActivity {
    private ViewStub viewstub_mainguide;

    private View mainguide_stub;

    private SharedPreferences set_prefs;

    private MyApplication appdata;

    private View top_main_bottom;
    
    public static final String MICROHOT = "microhot";
    
    public static final String COLUMN_ID = "column_id";

    public static final String POSITION = "position";
    
    public static final String CUSTOM_FINISH = "custom_finish";
    
    private HomeMainFragment mMainFragment;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        appdata = (MyApplication) this.getApplicationContext();
        appdata.setExit(false);
        
        
        setContentView(R.layout.mainguide);
        findViewsById();

        set_prefs = PreferenceManager.getDefaultSharedPreferences(this);
        

        if (set_prefs.getBoolean("newsreaderguide", true)) {
            mainguide_stub = viewstub_mainguide.inflate();
            ImageView guideview = (ImageView) mainguide_stub.findViewById(R.id.news_guide);
            guideview.setBackgroundResource(R.drawable.mainguideinfo);
            mainguide_stub.setOnClickListener(guideClickListener);
        } 
    }
    
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
       String pintent = intent.getStringExtra("pintent");
       if("pintent".equals(pintent)){
           NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
           mNotificationManager.cancel(R.drawable.stat_download_icon);
           mNotificationManager.cancel(R.layout.offline_finish_noti);
       }
    }

    @Override
	protected void onStart() {
		super.onStart();
		
		if (mMainFragment == null) {
		    mMainFragment = (HomeMainFragment) addFragmentByTag(R.id.newsreader_main_container,
	                HomeMainFragment.class, HomeMainFragment.TAG, null);
		    mMainFragment.setup();
		    mMainFragment.selectInTab();
		}
	}



	private void findViewsById() {	    
        viewstub_mainguide = (ViewStub) findViewById(R.id.viewstub_mainguide);
        top_main_bottom = findViewById(R.id.top_main_bottom);
        
        top_main_bottom.findViewById(R.id.go_back_common_btn).setVisibility(View.GONE);


        setListeners();

    }

    private void setListeners() {
        
        top_main_bottom.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mMainFragment != null) {
                    mMainFragment.dispatchActivityViewClick(v);
                }
            }

        });
    }

    private View.OnClickListener guideClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            set_prefs.edit().putBoolean("newsreaderguide", false).commit();
            if (null != mainguide_stub) {
                mainguide_stub.setVisibility(View.GONE);
                ImageView guideview = (ImageView) mainguide_stub.findViewById(R.id.news_guide);
                guideview.setBackgroundDrawable(null);
            }
        }
    };
    

    @Override
    protected void onResume() {
        super.onResume();
        String goHome = appdata.getGoHome();
        if ("Y".equals(goHome)) {
            // Log.i("goHome","goHome");
            if (mMainFragment != null) {
                mMainFragment.getTabHost().setCurrentTab(0);
            }

            appdata.setGoHome("N");
            if (set_prefs.getBoolean("newsreaderguide", true)) {
                // Log.v("newsreader","view_control:"+mainguide_stub);
                try {
                    if (null != mainguide_stub) {
                        mainguide_stub.setVisibility(View.VISIBLE);
                        ImageView guideview = (ImageView) mainguide_stub
                                .findViewById(R.id.news_guide);
                        guideview.setBackgroundResource(R.drawable.mainguideinfo);
                        mainguide_stub.setOnClickListener(guideClickListener);
                    } else {

                        mainguide_stub = viewstub_mainguide.inflate();
                        ImageView guideview = (ImageView) mainguide_stub
                                .findViewById(R.id.news_guide);
                        guideview.setBackgroundResource(R.drawable.mainguideinfo);
                        mainguide_stub.setOnClickListener(guideClickListener);

                        // set_prefs.edit().putBoolean("newsreaderguide",
                        // false).commit();
                    }
                } catch (java.lang.OutOfMemoryError error) {
                    set_prefs.edit().putBoolean("newsreaderguide", false).commit();
                    if (null != mainguide_stub) {
                        mainguide_stub.setVisibility(View.GONE);
                        ImageView guideview = (ImageView) mainguide_stub
                                .findViewById(R.id.news_guide);
                        guideview.setBackgroundDrawable(null);
                    }
                }

            } else {

            }
        }

    
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
       
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (set_prefs.getBoolean("newsreaderguide", true)) {
            if (event.getAction() == KeyEvent.ACTION_UP) {
                dismissGuide();
            }
            return true;
        }
        
        if (mMainFragment != null && mMainFragment.dispatchKeyEvent(event)) {
            return true;
        }

        return super.dispatchKeyEvent(event);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            showExit();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }
    
    private void dismissGuide() {
        set_prefs.edit().putBoolean("newsreaderguide", false).commit();
        if (null != mainguide_stub) {
            mainguide_stub.setVisibility(View.GONE);
            ImageView guideview = (ImageView) mainguide_stub.findViewById(R.id.news_guide);
            guideview.setBackgroundDrawable(null);
        }
    }

   
 
    public void goToMyCollect() {
        Intent intent = new Intent(HomePage.this, CollectActivity.class);
        startActivity(intent);
    }

    public void goToSetting() {
        Intent intent = new Intent(HomePage.this, SettingsActivity.class);
        startActivity(intent);
    }

    public void changeTheme() {
        ThemeSettingsHelper themeSettingsHelper = getThemeSettingsHelper();
        if (themeSettingsHelper.isNightTheme(this)) {
            themeSettingsHelper.changeTheme(HomePage.this, ThemeSettingsHelper.THEME_DEFAULT);
        } else {
            themeSettingsHelper.changeTheme(HomePage.this, ThemeSettingsHelper.THEME_NIGHT);
        }
    }

    public void goToMyComment() {
        Intent intent = new Intent(HomePage.this, MyCommentsActivity.class);
        startActivity(intent);
    }

    public void showExit() {
        new AlertDialog.Builder(HomePage.this).setTitle("").setMessage("确认退出网易新闻吗？")
                .setPositiveButton("退出", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialoginterface, int j) {
                  
                        try {
                            /*Intent clearCacheIntent = new Intent(HomePage.this,
                                    ClearCacheService.class);
                            startService(clearCacheIntent);*/

                        } catch (Exception e) {
                        }
                        appdata.setExit(true);
                        HomePage.this.finish();
                    }
                }).setNegativeButton("取消", null).show();
    }

 
   

    @Override
    public void onApplyTheme() {
        super.onApplyTheme();
        ThemeSettingsHelper themeSettingsHelper = getThemeSettingsHelper();
        
        TextView top_main_text = (TextView) top_main_bottom.findViewById(R.id.top_main_text);
        
        themeSettingsHelper.setTextViewColor(this, top_main_text, R.color.main_title_color);
        
        ImageView top_id_image = (ImageView) top_main_bottom.findViewById(R.id.top_id_image);
        themeSettingsHelper.setImageViewSrc(this, top_id_image, R.drawable.netease_top);
        
        themeSettingsHelper.setViewBackgroud(this, top_main_bottom, R.drawable.top_background);

        themeSettingsHelper.setViewBackgroudColor(this, R.id.newsreader_container, R.color.main_bg_color);


        if (mMainFragment != null) {
            mMainFragment.applyTheme();
        }
    }
}
